package com.fsnbigdata.repository;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.PhenologyRecord;
import com.fsnbigdata.domain.PlantConservation;
import com.fsnbigdata.domain.QPhenologyRecord;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * = PhenologyRecordRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = PhenologyRecordRepositoryCustom.class)
@Transactional(readOnly = true)
public class PhenologyRecordRepositoryImpl extends QueryDslRepositorySupportExt<PhenologyRecord> implements PhenologyRecordRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    PhenologyRecordRepositoryImpl() {
        super(PhenologyRecord.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LOCATION = "location";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String OBSERVE_YEAR = "observeYear";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_FOUR = "phaseFour";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_ONE = "phaseOne";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_THREE = "phaseThree";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_FIVE = "phaseFive";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_SIX = "phaseSix";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PLANT_CONSERVATION = "plantConservation";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String OBSERVER = "observer";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String INTRO_NO = "introNo";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String PHASE_TWO = "phaseTwo";

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantConservation
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<PhenologyRecord> findByPlantConservation(PlantConservation plantConservation, GlobalSearch globalSearch, Pageable pageable) {
        QPhenologyRecord phenologyRecord = QPhenologyRecord.phenologyRecord;
        JPQLQuery<PhenologyRecord> query = from(phenologyRecord);
        Assert.notNull(plantConservation, "plantConservation is required");
        query.where(phenologyRecord.plantConservation.eq(plantConservation));
        Path<?>[] paths = new Path<?>[] { phenologyRecord.plantConservation, phenologyRecord.introNo, phenologyRecord.location, phenologyRecord.observeYear, phenologyRecord.observer, phenologyRecord.phaseOne, phenologyRecord.phaseTwo, phenologyRecord.phaseThree, phenologyRecord.phaseFour, phenologyRecord.phaseFive, phenologyRecord.phaseSix };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(PLANT_CONSERVATION, phenologyRecord.plantConservation).map(INTRO_NO, phenologyRecord.introNo).map(LOCATION, phenologyRecord.location).map(OBSERVE_YEAR, phenologyRecord.observeYear).map(OBSERVER, phenologyRecord.observer).map(PHASE_ONE, phenologyRecord.phaseOne).map(PHASE_TWO, phenologyRecord.phaseTwo).map(PHASE_THREE, phenologyRecord.phaseThree).map(PHASE_FOUR, phenologyRecord.phaseFour).map(PHASE_FIVE, phenologyRecord.phaseFive).map(PHASE_SIX, phenologyRecord.phaseSix);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, phenologyRecord);
    }
}
